#!/usr/bin/make -f
default: help
	pwd

license_delay?=10
project_makefile?=./tizen.mk
# TODO overload/update with your variables if needed
cert?=tmp
user?=${USER}
name?=${user}
url?=http://${user}.localhost
email?=${user}@${user}.localhost
sdbhost?=sdbhost

# TODO: FIXME
samsung_cert_dir?=${HOME}/SamsungCertificate/${cert}

cert_name?=${cert}
cert_password?=password
cert_alias?=${cert}
cert_email?=${email}
cert_dir?=${keystore_dir}/${cert_alias}
cli_cert_dir?=${keystore_dir}/tmp
cert_author_file?=${cert_dir}/author.p12
cert_distributor_file?=${tizen_studio}/tools/certificate-generator/certificates/distributor/tizen-distributor-signer.p12
cert_ca_file?=${tizen_studio}/tools/certificate-generator/certificates/distributor/tizen-distributor-ca.cer
# In Configuration, Set a default profile path to
cli_cert_profiles_file?=${tizen_data}/ide/keystore/profiles.xml
workspace?=${HOME}/workspace
ide_cert_profiles_file?=${workspace}/.metadata/.plugins/org.tizen.common.sign/profiles.xml

cert_profiles_file?=${cert_dir}/profiles.xml

# TODO: FIXME
ifneq (${cert},tmp)
cert_dir=${samsung_cert_dir}
src_cert_profiles_file=${ide_cert_profiles_file}
export cert_dir
endif
src_cert_profiles_file?=${cli_cert_profiles_file}

target_home?=/home/developer

# TODO: update to latest if stable
tizen_studio_version?=1.0.1
tizen_studio_os?=$(shell /usr/bin/arch \
 | grep -q 'x86_64' && echo "ubuntu-64" || echo "ubuntu-32")
tizen_studio_url?=http://download.tizen.org/sdk/Installer/tizen-studio_${tizen_studio_version}/web-cli_Tizen_Studio_${tizen_studio_version}_${tizen_studio_os}.bin
tizen_studio_file?=$(shell basename -- "${tizen_studio_url}")
tizen_package_manager?=${tizen_studio}/package-manager/package-manager-cli.bin
tizen_studio?=${HOME}/tizen-studio
tizen_ide?=${tizen_studio}/ide/TizenStudio.sh
tizen_studio_log?=${tizen_studio}/tools/ide/ncli.log
tizen_data?=${HOME}/tizen-studio-data
keystore_dir?=${HOME}/tizen-studio-data/keystore
tizen?=${tizen_studio}/tools/ide/bin/tizen
sdb?=${tizen_studio}/tools/sdb
gbs_profile?=tizen_${app_profile_version_alpha}_${gbs_arch}

project_type?=$(shell ls config.xml >/dev/null 2>&1 \
 && echo "web" || echo "native")

ifeq (${project_type}, native)
project_type_first?=Native
### Native variables ###
compiler?=gcc
#arch_family?=x86
#arch_suffix?=i386
arch_family?=arm
arch_suffix?=${arch_family}
build_configuration?=Release
#build_configuration=Debug
build_dir?=./${build_configuration}
manifest_file?=tizen-manifest.xml

package_name?=$(shell \
 xmllint --format ${manifest_file} \
 | grep -o '<manifest .*'  \
 | grep -o 'package=.*'  | cut -d'"' -f2\
 || echo "org.example.main" \
)

app_package_version?=$(shell \
 xmllint --format ${manifest_file} \
 | grep -o '<manifest .*'  \
 | grep -o ' version=.*'  | cut -d'"' -f2\
 || echo "0.0.0" \
)

# TODO:
pkgid=${package_name}
#app_package_version=1.0.0
#app_package_exe?=main
#project_name=com.example.${app_package_exe}
pkg_type?=tpk
pkg_file?=${build_dir}/${package_name}-${app_package_version}-${arch_suffix}.tpk
log_tag?=${project_name}

# TODO: Error: <name> not pkg_id 
project_name?=$(shell echo ${pkgid} | sed -e 's|.*\.\([^.]*\)|\1|g' )
project_template?=basic-ui
project_template_dir?=${tizen_studio}/platforms/tizen-${app_profile_version}/${app_profile}/samples/Template/Native/${project_template}
ide_tizen_studio_package?=${app_profile_upcase}-${app_profile_version}-NativeAppDevelopment
cli_tizen_studio_package?=${ide_tizen_studio_package}-CLI
# MOBILE-2.4-NativeAppDevelopment-CLI : NativeToolchain-Gcc-4.9
tizen_studio_toolchain_package?=NativeToolchain-Gcc-4.9

#TODO: should be there if not you can create from this template
define manifest
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<manifest xmlns="http://tizen.org/ns/packages" api-version="2.4" package="${app_package_name}" version="${app_package_version}">
    <author email="${email}" href="${url}">${name}</author>
    <description>${app_description}</description>
    <profile name="${app_profile}"/>
    <ui-application appid="${app_pacakage_name}" exec="${app_package_exe}" launch_mode="single" multiple="false" nodisplay="false" taskmanage="true" type="capp">
        <label>${app_label}</label>
        <icon>${icon_file}</icon>
    </ui-application>
    <privileges>
        <privilege>http://tizen.org/privilege/internet</privilege>
    </privileges>
    <feature name="http://tizen.org/feature/screen.size.all">true</feature>
</manifest>
endef
export manifest

else # web
project_type_first?=Web
#TODO
build_dir?=.

define manifest
<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:tizen="http://tizen.org/ns/widgets"  id="http://example.com/main" version="1.0.0" viewmodes="maximized">
  <tizen:application id="ComExample.main" package="ComExample" required_version="2.4"/>
  <content src="${index_html_file}"/>
  <feature name="http://tizen.org/feature/screen.size.all"/>
  <name>main</name>
    <tizen:privilege name="http://tizen.org/privilege/filesystem.read"/>
    <tizen:privilege name="http://tizen.org/privilege/filesystem.write"/>
    <tizen:privilege name="http://tizen.org/privilege/internet"/>
  <tizen:setting context-menu="disable"/>
  <tizen:profile name="mobile"/>
</widget>
endef
export manifest

manifest_file?=config.xml

package_id?=$(shell \
 xmllint --format ${manifest_file} \
 | grep -o '<tizen:application .*'  \
 | grep -o 'id=.*'  | cut -d'"' -f2\
 || echo "OrgExample.main" \
)

pkgid=$(shell echo ${package_id} | cut -d. -f1)

# TODO: Error: <name> not pkg_id 
project_name?=$(shell echo ${pkgid} | sed -e 's|.*\([^.]*\)|\1|g' )

app_package_version?=$(shell \
 xmllint --format ${manifest_file} \
 | grep -o '<widget .*'  \
 | grep -o ' version=.*'  | cut -d'"' -f2\
 || echo "1.0.0" \
)

package_name?=$(shell \
 xmllint --format ${manifest_file} \
 | grep -o '<name>.*</name>'  \
 | sed -e 's|.*>\(.*\)<.*|\1|g' \
 || echo "OrgExample" \
)
pkg_type?=wgt
pkg_file?=${package_name}.${pkg_type}

#pkgid?=ComExample
index_html_file?=index.html

config.xml:
	-ls ${index_html_file}
	echo $${manifest} > $@.tmp
	xmllint --format $@.tmp > $@
	rm $@.tmp

endif

# TODO:
app_description?=TODO
app_label?=TODO
app_profile?=mobile
app_profile_upcase?=MOBILE
app_profile_version?=2.4
app_profile_version_alpha?=2_4
tizen_sdk_profile?=${app_profile}-${app_profile_version}
icon_file=icon.png

export app_profile_version
export app_profile
export project_template
#TODO: needed?
export cli_tizen_studio_package

# TODO: if dependencies add them
# srcs+=usr/lib usr/include

### Generic variables ###

self?=$(lastword $(MAKEFILE_LIST))
thisdir?=$(shell dirname $(realpath ${self}))
make?=${MAKE} -f ${self}
export make

date?=$(date -u %s)
export cert_dir


### Generic Rules ###

%: rule/%
	echo "#@: $^"

rule/tmp/rule/%: rule/%
	echo "#@: $^"

tmp/rule/%:
	ls $@ || ${make} rule/$@
	mkdir -p ${@D}
	touch ${@}

help: tizen/help rule/print tmp/rule/pre-check
	pwd


rule/print:
	@echo "### Current configuration:"
	@echo "# project_type=${project_type}"
#
	@echo "# package_name=${package_name}"
	@echo "# app_package_version=${app_package_version}"
	@echo "# pkgid=${pkgid}"
	@echo "# project_name=${project_name}"
	@echo "# pkg_file=${pkg_file}"
#
	@echo "# cert_author_file=${cert_author_file}"
	@echo "# cert_distributor_file=${cert_distributor_file}"
	@echo "# ide_profiles_file=${ide_profiles_file}"
	@echo "# cert_profiles_file=${cert_profiles_file}"
	@echo "# cert_ca_file=${cert_ca_file}"
	@echo "# self=${self}"
	@echo "# make=${make}"
	@echo "# project_template_dir=${project_template_dir}"

rule/type/%: rule/${project_type}/%
	pwd

tmp/rule/pre-check: rule/pre-check-host rule/pre-check-tizen
	pwd
	mkdir -p ${@D}
	touch ${@}

rule/pre-check-host:
	@echo "### checking needed tools:"
	which convert
	which unzip
	which wget

rule/pre-check-tizen: ${tizen} ${project_template_dir}
	ls $<

tizen/help: ${tizen}
	${tizen}  cli-config -l

all: ${all}
	ls -l -- $^

### Tizen sdb ###

run-app:
	${sdb} shell pkgcmd -l | grep ${package_name} || ${make} deploy
	${sdb} shell open_app ${package_name}


reboot:
	${sdb} root on || echo "TODO: must do it manually"
	${sdb} shell /sbin/reboot || echo "TODO: reboot if not done"
	sleep 20
	echo "should be booting"
	sleep 20
	echo "should be booted"
	sleep 10
	${make} connect || sleep 60
	${make} connect || sleep 60
	${make} connect || sleep 120

connect:
	${sdb} devices
	${sdb} devices | grep 'offline' && sdb kill-server || :
	${sdb} devices
	${sdb} devices | grep 'unknown' && sleep 10 || :
	${sdb} get-serialno
	${sdb} get-serialno | grep 'target not exist' && sleep 10 || :
	${sdb} get-state | grep 'device' || sleep 20
	${sdb} shell cat /proc/uptime

connect-more:
	${sdb} kill-server
	-killall -9 sdb
	${sdb} start-server
	sleep 10
	${make} connect
	${sdb} get-state | grep 'offline' && ${sdb} status-window || :
	${sdb} shell id

connect-host:
	${sdb} connect ${sdbhost}


### Tizen Tools ###

rule/tizen_studio: ${tizen}
	ls $<

${tizen}:
	ls $@ || ${make} rule/tizen_studio/install

rule/tizen_studio/install/tizen: tmp/${tizen_studio_file}
	-ls /etc/debian_version || echo "#warning: non debian system!"
	chmod u+rx $<
	file $<
	${<D}/${<F} --no-java-check --show-license
	@echo "# About to accept license, Press Ctrl+C now to reject it"
	sleep ${license_delay}
	touch ~/.bashrc
	${<D}/${<F} --no-java-check --accept-license "${tizen_studio}"
	ls "${tizen}"

rule/tizen_studio/install: rule/tizen_studio/install/tizen ${project_template_dir}
	pwd

${tizen_package_manager}:
	${make} rule/tizen_studio/install
	ls -l $@

tmp/${tizen_studio_file}:
	mkdir -p ${@D}
	wget -c -O "${@}" ${tizen_studio_url}

rule/tizen/installed/%: ${tizen_studio}/local/tmp/installed/%
	ls $<

rule/tizen/show-pkgs: ${tizen_package_manager}
	${<} --no-java-check show-pkgs

${tizen_studio}/local/tmp/installed/%: ${tizen_package_manager}
	${<} --no-java-check show-pkgs | grep -- "^i .*${@F}" \
 || ${make} rule/tizen/install/${@F}
	mkdir -p ${@D}
	touch $@

rule/tizen/install/%: ${tizen_package_manager}
	${<} --no-java-check show-pkgs | grep -- "${@F}"
	${<} --no-java-check show-license ${@F}
	@echo "# About to accept license, Press Ctrl+C now to reject it"
	sleep ${license_delay}
	${<} --no-java-check install ${@F} --accept-license

${project_template_dir}:
	${make} rule/tizen/installed/${cli_tizen_studio_package}
	ls ${@}

rule/tizen/cli: rule/tizen/show-pkgs \
 rule/tizen/installed/cert-add-on \
 rule/tizen/installed/${cli_tizen_studio_package}
	ls -l -- ${tizen}

rule/tizen/ide: rule/tizen/installed/${ide_tizen_studio_package} \
 rule/tizen/ide/${project_type} \
 rule/tizen/cli \
 rule/tizen/installed/PlatformIDE
	ls ${tizen_ide}

rule/ide:
#	ls ${tizen_ide} || make rule/tizen/ide
	${tizen_ide}

### Certificates ###

${cert_author_file}:
	${make} rule/tizen/cli-cert_author_file
	ls $@

rule/tizen/cli-cert_author_file:
	ls ${tizen} || ${make} ${tizen}
	@echo "# About to generate: $@"
	@mkdir -p  ${cli_cert_dir}
	${tizen} certificate \
 -a "${cert_alias}" \
 -p "${cert_password}" \
 -n "${cert_name}" \
 -e "${cert_email}" \
 -- \
 ${cli_cert_dir}
 #eol
	ls -l -- ${cert_author_file}
	${RM} -f ${ide_cert_profiles_file}

${cli_cert_profiles_file}: ${cert_author_file}
	${make} rule/tizen/cli_cert_profile_file
	ls -- $@

${cert_profiles_file}: ${cert_author_file}
	ls -l -- $<
	rm -f ${cert_profiles_file}
	${tizen} cli-config "profiles.path=${cert_profiles_file}"
#	-rm -v ${tizen_studio}/tools/.tizen-cli-config
#	-rm -v ${cli_cert_profiles_file}
#	-rm -v ${tizen_studio}/tools/ide/conf/profiles.xml
	${tizen} cli-config -l
	${tizen} security-profiles add \
 -f \
 -n "${cert_name}" \
 -a $< \
 -p "${cert_password}" \
 #eol

${cli_cert_dir}: ${cli_cert_profiles_file}
	cp $< ${@D}/

rule/tizen/cli_cert_profile_file: ${cert_profiles_file}
	@echo "# Override"
	cp -v ${cert_profiles_file} ${cli_cert_profiles_file}
	${tizen} cli-config "profiles.path=${cli_cert_profiles_file}"
	grep -- "${cert}" ${cli_cert_profiles_file}

rule/tizen/cert-rm:
	rm -v ${cert_author_file} ${cli_cert_profiles_file}

rule/tizen/cert-cli: ${cli_cert_dir}
	ls $<

#	mv ${cli_profiles_file} $@
#	-cp -av ${cert_distributor_file} ${@D}
#	cp -av ${cert_ca_file} ${@D}
#	ls -l -- $@
#	@echo "track this dir $@ (I use git)"


# ide

todo/${ide_profiles_file}: ${cert_profiles_file}
	ls -l -- $<
	cp -v $< $@

#${cert_dir}:
#	${make} rule/tizen/cert-ide

todo/rule/tizen/cert-prep:
	ls ${samsung_cert_dir} 

#${cert_profiles_file}: ${cli_cert_profiles_file} ${cert_dir}
#	grep "${cert}" "${ide_cert_profiles_file}" && cp -v $< ${cert_dir}/ \
# || ${make} rule/cert/cert-ide
#	ls -l -- $@


todo/rule/cert/profile_file: ${cli_cert_profiles_file}

todo/rule/cert/ide_profile_file: ${ide_cert_profiles_file}
	grep ${cert} $<
	cp -v $< ${cert_dir}/
	ls -l -- ${cert_profiles_file}

todo/rule/cert: ${tizen} ${ide_profiles_file} ${cert_author_file} ${cert_distributor_file}
	grep "${cert}" ${ide_profiles_file} \
 || ${make} rule/cert/clean rule/cert/ide
	ls -l -- ${cert_profiles_file}

todo/rule/cert/ide: ${ide_profiles_file}

#${cert_profiles_file}: ${src_cert_profiles_file}
#	cp $< $@

#${cert_profiles_file}: ${ide_cert_profiles_file}
#	cp $< $@


rule/cert/select: ${cli_cert_profiles_file} ${tizen}
	${tizen} cli-config -l
	${tizen} cli-config "profiles.path=$<"
	${tizen} cli-config -l

rule/cert/select-cli: ${cert_profiles_file} ${tizen}
	cp -v ${cert_profiles_file} ${cli_cert_profiles_file} 
	${make} rule/cert/select
	grep "profile name=\"${cert}\"" ${cli_cert_profiles_file} 
#	cp -a ${cli_cert_profiles_file} ${cert_profiles_file}

rule/tizen/cert-select: ${cert_profiles_file} ${cert_dir}
	grep "profile name=\"${cert}\"" "${cert_profiles_file}" \
 || { rm -v ${cli_cert_profiles_file} && ${make} rule/cert/select-cli ; }
	grep "profile name=\"${cert}\"" ${cert_profiles_file} 
	ls $^

rule/tizen/cert-replace: ${cert_profiles_file}
	mkdir -p "${cli_cert_profiles_file}.tmp" #TODO
	cp -av "${<}" ${cli_cert_profiles_file}
	grep "profile name=\"${cert}\"" "$<"
	${tizen} cli-config "profiles.path=${cli_cert_profiles_file}"

rule/attempt:
	${make} rule/run \
|| rm -f ${cert_profiles_file}
	${make} rule/tizen/cert-ide rule/run

rule/cert/clean: ${tizen}
	-mv -vf ${ide_cert_profiles_file} ${ide_cert_profiles_file}._${date}.tmp
	-mv -vf ${cli_cert_profiles_file} ${cli_cert_profiles_file}._${date}.tmp
	rm -rf ${cert_dir}
	rm -rf .sign ${build_dir} Debug Release

rule/cert/check: ${sdb} rule/tizen/cert-select ${cert_profiles_file} rule/target/check
	grep "profile name=\"${cert}\"" ${cert_profiles_file}

rule/target/check:
	@echo "$@: { TODO: sdb don't exit on failure"
	${sdb} shell ls ${target_home}/certificate-registration.xml
	${sdb} shell ls ${target_home}/*.xml
	${sdb} shell ls ${target_home}/
	${sdb} shell ls ${target_home}/sdk_tools/
	@echo "$@: } failures can be ignored now"

${workspace}: 
	ls $@ || ${make} rule/tizen/workspace

rule/tizen/workspace: ${tizen_studio}/ide/TizenStudio.sh rule/tizen/installed/cert-add-on
	@echo "TODO"
	${<}
	ls -l -- $@

rule/workspace: ${workspace}
	ls -l -- $<


rule/ide_cert_profiles_file: 
	ls ${ide_cert_profiles_file} || ${make}  ${ide_cert_profiles_file}
	ls $<

${ide_cert_profiles_file}: tmp/rule/tizen/cert-ide ${cert_dir}
	ls $@

rule/tizen/cert-ide: ${tizen_ide} ${workspace}
	@echo "# Please Install and Setup IDE SDK & cert-addons"
	@echo "# https://wiki.tizen.org/wiki/SDK "
	@echo "https://wiki.tizen.org/wiki/SDK"
	@echo '# tools / Certificate manager/'
	@echo '# + / Samsung'
	@echo '# Mobile / Wearable'
	@echo '# new certificate profile : $name'
	@echo '# Select an existing author'
	@echo '# Your new distributor certificate is located in [HOME/SamsungCertificate/name]'
	${<D}/${<F}
	ls -l -- ${ide_cert_profiles_file}
	cp -v ${ide_cert_profiles_file} ${cert_dir}/
	ls ${cert_profiles_file}
	cp ${cert_profiles_file} ${cli_cert_profiles_file}
	${make} rule/cleanall
#TODO: try to run without certs first

rule/cleanall:
	rm -rf *.tpk *.wgt .buildResult
	rm -rf author-signature.xml .buildResult .manifest.tmp signature1.xml

### Native rules ###

ifeq (${project_type}, native)
rule/${project_type}/clean:
	rm -rf  tmp/rule

rule/${project_type}/distclean:
	rm -rf *.tpk author-signature.xml .buildResult .manifest.tmp signature1.xml tmp

Build: ${project_template_dir}
	mkdir -p tmp
	cd tmp && ${tizen} list ${project_type}-project 
	cd tmp && ${tizen} create ${project_type}-project \
 -p ${tizen_sdk_profile} -t ${project_template} -n ${project_name}
	mv tmp/${project_name}/Build $@

rule/${project_type}/build: Build ${tizen} ${project_template_dir} ${srcs} ${manifest_file} rule/${project_type}/import/clean 
	@rm -rf ${build_dir}
	-@mv ${tizen_studio_log} ${tizen_studio_log}._${date}.bak
	@echo "# less ${tizen_studio_log} # on issues "
	${tizen} build-native \
 -a ${arch_family} -c ${compiler} -C ${build_configuration} -- ${CURDIR}
	ls -l ${build_dir}/objs
	cat ${build_dir}/build.info

rule/${project_type}/import/clean:
	@echo "If some libs were imported inplace"
	-rm -rf usr/lib/*.a lib/*.a lib/pkgconfig bin

rule/${project_type}/help:
	echo "# app_package_version=${app_package_version}"

#${pkg_file}: rule/${project_type}/package
#	ls -l -- $@
#TODO factorize
#rule/${project_type}/package: rule/${project_type}/build rule/cert/check
#TODO
#	${tizen} package -t ${pkg_type} -s ${cert} --  "${build_dir}"
#	ls -l -- "${build_dir}/"*.${pkg_type}

# aliases

#rule/tizen/ide/native: rule/tizen/ide rule/tizen/installed/MOBILE-2.4-NativeAppDevelopment

rule/tizen/ide/native: rule/tizen/installed/NativeIDE \
 rule/tizen/installed/${tizen_studio_toolchain_package}
	pwd

else # // wgt

rule/${project_type}/build: .buildResult
	ls $<

rule/${project_type}/clean:
	-rm -rf .buildResult
	-rm -rf *.tmp author-signature.xml signature1.xml .manifest.tmp

rule/${project_type}/distclean: rule/${project_type}/clean
	-rm -rf *.wgt tmp
endif

.buildResult: ${manifest_file} ${tizen} tmp/rule/tizen/install/${project_type_first}CLI
	@echo "# TODO: check if $^ needed"
	${tizen} build-${project_type} --exclude '.git/* tmp/*'
	ls $@

#TODO factorize
rule/${project_type}/package: rule/${project_type}/build rule/tizen/cert-replace
	ls ${build_dir}
	${tizen} cli-config -l
	ls ${cert_dir}
	-rm -rfv author-signature.xml signature1.xml
	-rm -rfv .sign/*
	${tizen} package -t ${pkg_type} -s "${cert}" -- "${build_dir}"
	ls -l -- ${tizen_studio}/tools/ide/ncli.log
	ls -l -- ${pkg_file}
	touch ${pkg_file}

rule/${project_type}/${pkg_type}: ${pkg_file}
	@ls -l -- $^
	md5sum $^

undeploy:
	-${sdb} shell pkgcmd -l -t ${pkg_type}
	-${sdb} shell pkgcmd -u -n ${package_name}
	-${sdb} shell rm /opt/usr/apps/tmp/${<F}
	${sdb} shell pkgcmd -l -t ${pkg_type} | grep "${package_name}" \
 && exit 1 || exit 0


redeploy: undeploy deploy
	echo "# $@: $^ : done"


tmp/rule/redeployed/${pkg_file}: ${pkg_file}
	${make} redeploy
	@mkdir -p ${@D}
	touch $@


tmp/rule/deployed/${pkg_file}: redeploy
	@mkdir -p ${@D}
	touch $@

tmp/rule/deployed: tmp/rule/deployed/${pkg_file}
	ls -l -- $<

rule/${project_type}/deployed: tmp/rule/deployed/${pkg_file}
	echo "# $@: $^ : done"


${pkg_file}: rule/${project_type}/package
	ls -l $@

rule/${project_type}/check: ${pkg_file}
	unzip -t $<
	ls -l -- ${CURDIR}/${<}
	md5sum ${CURDIR}/${<}

rule/${project_type}/undeploy:
	-${sdb} shell pkgcmd -l -t ${pkg_type}
	-${sdb} shell pkgcmd -u -n ${pkgid}

rule/${project_type}/deploy: ${pkg_file} rule/${project_type}/undeploy
	-${sdb} shell rm /opt/usr/apps/tmp/${<F}
	${tizen} install -n ${<F} -- ${CURDIR}/${<D}
	${sdb} shell ls -l /opt/usr/apps/tmp/${<F}
	-${sdb} shell pkgcmd -l -t ${pkg_type} | grep "${pkgid}"
	${sdb} shell ls /opt/usr/apps/${package_name}
	-${sdb} shell ls /opt/usr/apps/${package_name}/lib

#rule/${project_type}/run: tmp/rule/deployed ${tizen}
#	${tizen} run --pkgid "${project_name}" -- .

rule/run: rule/type/deploy ${tizen}
	${tizen} run --pkgid ${pkgid} -- .

rule/distclean: rule/type/distclean

### Updated rules ####
distclean: rule/${project_type}/distclean
	pwd

%: rule/${project_type}/%
	pwd

rule/package: rule/${project_type}/${pkg_type}
	@echo "# $@: $^"

### MISC ###

shared/res/main.png: icon.png
	cp -v $< $@

icon.png: tmp/117x177.png
	convert -resize 117x117! $< $@

docs/logo.png:
	cp docs/screenshot.png $@

tmp/screenshot.jpg: docs/screenshot.png
	convert $< $@

tmp/117x177.png: docs/logo.png
	mkdir -p ${@D}
	convert -resize '117x117!' $< $@

tmp/512x512.png: docs/logo.png
	mkdir -p ${@D}
	convert -resize '512x512!' $< $@

tmp/512x512.jpg: docs/logo.png
	mkdir -p ${@D}
	convert -resize '512x512!' $< $@

tmp/480x854/%.png: docs/%.png
	mkdir -p ${@D}
	convert -resize '480x854!' $< $@

rule/setup/debian: /etc/debian_version
	sudo apt-get install \
 file \
 git \
 graphicsmagick-imagemagick-compat \
 make \
 rpm \
 sudo \
 time \
 unp \
 wget \
 zip \
 #eol

rule/setup/fedora: /etc/fedora-release
	sudo yum install \
 file \
 git \
 make \
 rpm \
 sudo \
 time \
 wget \
 zip \
 #eol

rule/tizen-helper/%: bin/tizen-helper.sh
	${CURDIR}/${<} ${@F}

rule/prep:
	@echo "# Checking system and prepare it"
	@echo "# MAKEFLAGS=${MAKEFLAGS} # should be <= -j1"
	which gbs || ${make} rule/tizen-helper/setup

#TODO
rule/samsung:
	ln -fs ~/SamsungCertificate/tmp-sam ~/tizen-studio-data/keystore/
	cp ${cert_dir}/profiles.xml ${ide_cert_profiles_file}
	${tizen} cli-config "profiles.path=${ide_cert_profiles_file}"

rule/release: rule/distclean rule/run
	md5sum ${pkg_file}

tizen-manifest.xml:
	@echo "# TODO: $@"
	touch $@


rule/sdb/%: ${sdb}
	${<} ${@F}

.PHONY: tizen.mk
